package app.caihan.wanandroid.base

import app.caihan.scframe.base.viewmodel.BaseViewModel
import app.caihan.scframe.livedata.BooleanLiveData
import app.caihan.wanandroid.model.ScWanAndroidFactory
import app.caihan.wanandroid.model.bean.PageList


abstract class WanBaseViewModel : BaseViewModel() {
    private val repository by lazy { ScWanAndroidFactory.makeWanRepository() }

    //SmartRefreshLayout 下拉刷新布局参数 start
    var curPage = 0
    val isRefreshOrLoadMoreFinish = BooleanLiveData()
    val hasMoreData = BooleanLiveData()
    val autoRefresh = BooleanLiveData()
    //SmartRefreshLayout 下拉刷新布局参数 end


    /**
     * onRefreshListener/onLoadMoreListener 接口调用完之后需要调用一下该接口
     * 首次网络请求成功后需要判断数据shif
     *
     * @param hasMore 是否还有更多数据
     */
    fun finishRefresh(listObj: PageList?, isRefresh: Boolean = false) {
        isRefreshOrLoadMoreFinish.value = true
        if (listObj != null) {
            if (isRefresh) {
                hasMoreData.value = !listObj.datas.isNullOrEmpty()
            } else {
                hasMoreData.value = listObj.curPage <= listObj.pageCount
            }
        } else {
            if (isRefresh) {
                hasMoreData.value = false
            }
        }
//        isRefreshOrLoadMoreFinish.value = false
    }

//    fun autoRefresh(){
//        autoRefresh.value = true
//    }
}